/* global window */
import modelExtend from 'dva-model-extend'
import * as operatorService from './services/operator'
import { pageModel } from 'utils/model'
import * as employeeService from "../employee/services/employees"
import * as departmentService from '../department/services/departments'
import * as dictionaryService from 'services/dictionary'

const { query, remove, create, update, operatorStat } = operatorService

export default modelExtend(pageModel, {
  namespace: 'operator',

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    expandForm: false,
    selectedRowKeys: [],
    stat: [],
    empList: [],
    departments: [],
    figureIp: [],
    friendCircleType: [],
    source: [],
    operateStatus: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/operator') {
          const payload = location.query || { current: 1, size: 10 }
          dispatch({
            type: 'query',
            payload,
          })
          dispatch({
            type: 'init',
          })
        }
      })
    },
  },

  effects: {

    * init({ payload = {} }, { call, put }) {
      const statResp = yield call(operatorStat, payload)
      const deptResp = yield call(departmentService.query)
      const figureIpResp = yield call(dictionaryService.queryBySn, { sn: 'figure_ip' })
      const friendCircleTypeResp = yield call(dictionaryService.queryBySn, { sn: 'friend_circle_type' })
      const sourceResp = yield call(dictionaryService.queryBySn, { sn: 'source' })
      const operateStatusResp = yield call(dictionaryService.queryBySn, { sn: 'operate_status' })
      yield put({
        type: 'updateState', payload: {
          stat: statResp.data,
          departments: deptResp.data,
          figureIp: figureIpResp.data,
          friendCircleType: friendCircleTypeResp.data,
          source: sourceResp.data,
          operateStatus: operateStatusResp.data,
        },
      })
    },

    * query({ payload = {} }, { call, put }) {
      const response = yield call(query, payload)

      const { data } = response
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          },
        })
      }
    },

    * delete({ payload }, { call, put, select }) {
      const data = yield call(remove, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.operator)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
      } else {
        throw data
      }
    },

    * searchEmp({ payload = {} }, { call, put }) {
      const response = yield call(employeeService.searchEmp, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'updateState',
          payload: {
            empList: data,
          },
        })
      }
    },

    * multiDelete({ payload }, { call, put }) {
      const data = yield call(operatorService.remove, payload)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
      } else {
        throw data
      }
    },

    * create({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.code === 0) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },

    * update({ payload }, { select, call, put }) {
      const id = yield select(({ operator }) => {
        if (payload.id) {
          return payload.id
        }
        return operator.currentItem.id
      })

      const newDept = { ...payload, id }
      const data = yield call(update, newDept)
      if (data.success) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },

  },

  reducers: {

    showModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal(state) {
      return { ...state, modalVisible: false }
    },

    toggleForm(state) {
      const { expandForm } = state
      return {
        ...state,
        expandForm: !expandForm,
      }
    },
  },
})
